package com.rd.student.ruanjian.class3.gaochenxi.day0731;

import java.util.*;

public class TestTwoWeiRandom {
   public static void main(String[] args) throws InterruptedException {
      Scanner scanner = new Scanner(System.in);
      System.out.println("请输入想要的行列规格:（要求h、w均为整数,且h*w==20）");
      System.out.println("请输入行：");
      int h = scanner.nextInt();
      System.out.println("请输入列：");
      int w = scanner.nextInt();
      System.out.println("即将输出10组,loading...");
      Thread.sleep(2000);
//      Func1(h, w);
//      Func2(h, w);
      Func3(h, w);
   }

   //用ArrayList处理
   public static void Func1(int h, int w) {
      int min = 0;
      int max = 50;
      int[] arr = new int[20];
      int arr2[][] = new int[h][w];
      Random random2 = new Random();
      ArrayList<Integer> list = new ArrayList<>(arr.length);
      System.out.println("====规格5行4列====");
      for (int k = 0; k < 10; k++) {
         for (int i = 0; i < arr.length; i++) {
            Random random = new Random();
            int randoms = min + random.nextInt(max - min);
            arr[i] = randoms;
            if (list.contains(arr[i])) {
               i--;
               continue;
            }
            list.add(arr[i]);
         }
         for (int j = 0; j < list.size(); j++) {
            arr[j] = list.get(j);
            list.remove(j);
         }
         System.out.println("=====第" + (k + 1) + "组====");
         one_and_Two(arr, arr2);
      }
   }


   //用数组处理
   public static void Func2(int h, int w) {
      int min = 0;
      int max = 50;
      int[] arr = new int[20];
      int[][] arr2 = new int[h][w];
      //去重核心代码
      for (int k = 0; k < 10; k++) {
         for (int i = 0; i < arr.length; i++) {
            Random random = new Random();
            int randoms = min + random.nextInt(max - min);
            arr[i] = randoms;
            for (int j = 0; j < i; j++) {
               if (arr[j] == arr[i]) {
                  i--;
                  break;
               }
            }
         }
         System.out.println("=====第" + (k + 1) + "组====");
         one_and_Two(arr, arr2);
      }
   }


   //用hashSet处理
   public static void Func3(int h, int w) {
      int min = 0;
      int max = 50;
      int count = 0;
      int[] arr = new int[20];
      int[][] arr2 = new int[h][w];


      HashSet set = new HashSet();
      while (set.size() < 20) {
         Random random = new Random();
         int randoms = min + random.nextInt(max - min);
         set.add(randoms);
      }
      //怎么才能把值打进arr中通过set
      Iterator iterator = null;
      while (count < 20) {
         iterator = set.iterator();
         while (iterator.hasNext()) {
            Object next = iterator.next();
            arr[count] = (int) next;
            count++;
         }
      }

      iterator = set.iterator();
      while (iterator.hasNext()) {
         Object obj = iterator.next();
         iterator.remove();
      }

      one_and_Two(arr, arr2);

   }

   //转化成二维数组并打印的方法
   public static void one_and_Two(int[] one, int[][] Two) {
      int k = 0;
      int hang = Two.length;
      int lie = Two[0].length;
      for (int i = 0; i < hang; i++) {
         for (int j = 0; j < lie; j++) {
            Two[i][j] = one[k];
            k++;
         }
      }
      for (int i = 0; i < Two.length; i++) {
         for (int j = 0; j < Two[i].length; j++) {
            System.out.print(Two[i][j] + " ");
         }
         System.out.println();
      }
   }
}
